package com.zhuanyongjin.server;

import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.zhuanyongjin.dao.membersDao;
import com.zhuanyongjin.dao.yongjinDao;
import com.zhuanyongjin.dao.impl.membersDaoImpl;
import com.zhuanyongjin.dao.impl.yongjinDaoImpl;
import com.zhuanyongjin.table.Yongjin;
import com.zhuanyongjin.util.Calenda;
import com.zhuanyongjin.util.DataTools;

public class yongjinServlet extends HttpServlet {

	/**
	 * The doGet method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
        //终止时间是由用户设定吗？
		response.setContentType("text/html");
		PrintWriter out = response.getWriter();
		Yongjin y=new Yongjin();
		membersDao m=new membersDaoImpl();
		String first_mcid=request.getParameter("first_mcid");
		String second_mcid=request.getParameter("second_mcid");
		String province=request.getParameter("province");
		String city=request.getParameter("city");
		String zone=request.getParameter("zone");
		int provinceId=0;
		int cityId=0;
		int zoneId=0;
		int fId=0;
		int sId=0;
		List list1=m.getprovinceId(DataTools.HexStringhtoString(province));
		if(list1.size()>0){
			HashMap adu=(HashMap)list1.get(0);
		    provinceId=Integer.parseInt(adu.get("Id").toString());
		}
		List list2=m.getprovinceId(DataTools.HexStringhtoString(city));
		if(list2.size()>0){
			HashMap adu=(HashMap)list2.get(0);
		    cityId=Integer.parseInt(adu.get("Id").toString());
		}
		List list3=m.getprovinceId(DataTools.HexStringhtoString(zone));
		if(list3.size()>0){
			HashMap adu=(HashMap)list3.get(0);
		    zoneId=Integer.parseInt(adu.get("Id").toString());
		}
		List list4=m.getmodulId(DataTools.HexStringhtoString(first_mcid));
		if(list4.size()>0){
			HashMap adu=(HashMap)list4.get(0);
		    fId=Integer.parseInt(adu.get("Id").toString());
		}
		List list5=m.getmodulId(DataTools.HexStringhtoString(second_mcid));
		if(list5.size()>0){
			HashMap adu=(HashMap)list5.get(0);
		    sId=Integer.parseInt(adu.get("Id").toString());
		}
		String member_id=request.getParameter("member_id");
		int price=Integer.parseInt(request.getParameter("price"));
		int yj=Integer.parseInt(request.getParameter("yj"));
		String mobel=request.getParameter("mobel");
		String title=request.getParameter("title");
		String discription=request.getParameter("discription");
//		//开始时间
//		Date start_time = null;
//		SimpleDateFormat ft=new SimpleDateFormat();
//		String st=request.getParameter("start_time");
//		try{
//			start_time=ft.parse(st);
//		}catch(Exception ex){
//			System.out.println(ex.getMessage());
//		}
//		//结束时间
//		Date end_date = null;
//		String ut=request.getParameter("end_date");
//		try{
//			end_date=ft.parse(ut);
//		}catch(Exception ex){
//			System.out.println(ex.getMessage());
//		}
		Calenda C=new Calenda();
		String start_time=C.getNowTime().toString();
		String end_date=C.getNowTime().toString();
		String upTime=C.getNowTime().toString();
		String guige=request.getParameter("guige");
		String caizhi=request.getParameter("caizhi");
		double longitude=Double.parseDouble(request.getParameter("longitude"));
		double latitude=Double.parseDouble(request.getParameter("latitude"));
		String img_url=request.getParameter("img_url");
		y.setCaizhi(DataTools.HexStringhtoString(DataTools.HexStringhtoString(caizhi)));
		y.setCity(cityId);
		y.setStartTime(start_time);
		y.setFirst_mcid(fId);
		y.setGuige(DataTools.HexStringhtoString(guige));
		y.setIsOk(0);
		y.setMember_id(member_id);
		y.setMobel(mobel);
		y.setPrice(price);
		y.setProvince(provinceId);
		y.setSecond_mcid(sId);
        y.setEndDate(end_date);
        y.setUpTime(upTime);
        y.setYj(yj);
        y.setZone(zoneId);
        y.setIsDelete(0);
        y.setIs_complete(0);
        y.setLongitude(longitude);
        y.setLatitude(latitude);
        y.setImgUrl(img_url);
		yongjinDao yongjins = new yongjinDaoImpl();
		//用户添加任务
		int count=yongjins.addyongjin(y);
		System.out.println("count"+count);
		if(count!=-1){
			out.print("success");
		}
		else
		{
			out.print("error");
		}
		out.flush();
		out.close();
	}

	/**
	 * The doPost method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to post.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doGet(request, response);
	}

}
